package com.example.demo.controller;

import com.example.demo.dao.UserInfo;
import com.example.demo.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller //将对象存储至Spring中
public class UserController {
    //注入方法1:属性注入
    @Autowired
    private UserService userService;
    // @Qualifier("u1")
    @Resource(name = "u1")
    //注入userinfo
//    @Autowired
    private UserInfo userInfo;

    public void sayHi() {
        System.out.println("你好,UserController");
        userService.sayHi();
        System.out.println(userInfo);
    }
}

@Controller
class UserController2 {
    //注⼊⽅法2: 构造⽅法
    private UserService userService;

    @Autowired
    public UserController2(UserService userService) {
        this.userService = userService;
    }

    public void sayHi() {
        System.out.println("你好,UserController2");
        userService.sayHi();
    }
}

@Controller
class UserController3 {
    //注⼊⽅法3: Setter⽅法注⼊
    private UserService userService;

    //    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void sayHi() {
        System.out.println("你好,UserController3");
        userService.sayHi();
    }
}